package cn.tedu.atomic;

public class VolatileDemo {
    public static void main(String[] args) {
        Data d = new Data();
        d.i = 10;
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("A线程启动了");
                while (d.i == 10) {

                }
                System.out.println("i的值被改变, i现在不是10了");
                System.out.println("A线程结束了");
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("B线程启动了");
                    Thread.sleep(3000);
                    d.i = 50;
                    System.out.println("B线程结束了");
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }
        }).start();
    }
}

class Data {
    volatile int i;
}
